﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using sl.WeiBee;

namespace sl
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            BackgroundWorker backgroundWorker = new BackgroundWorker();
            backgroundWorker.DoWork += new DoWorkEventHandler(backgroundWorker_DoWork);
            backgroundWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backgroundWorker_RunWorkerCompleted);
            
            label1.Content = "waiting.";
            //backgroundWorker.RunWorkerAsync();
            //Action getResultAction = new Action(() =>
            //                                        {
            //                                            string tempresult = GetOneResult();// "temp result";
            //                                            this.Dispatcher.BeginInvoke(() =>
            //                                                                            {
            //                                                                                label1.Content = tempresult;
            //                                                                            });
            //                                        }
 
            //);
            //getResultAction.BeginInvoke(null, null);
            //getResultAction.Invoke();//.BeginInvoke(null, null);
            string url = "http://api.t.sina.com.cn/account/verify_credentials.xml?oauth_consumer_key=3587646579&oauth_nonce=3636566&oauth_signature_method=HMAC-SHA1&oauth_timestamp=1312015003&oauth_token=1c1b8e4800c0cc920df3cce7e24b3e66&oauth_version=1.0&oauth_signature=8Krw5M3Q8Pj75QoZ%2fx3%2f7D49Dl4%3d";//string.Empty; // TODO: Initialize to an appropriate value
            string postData = string.Empty;
            //result = Utility.WebRequestUseAsyncEnumerator(method, url, postData);
            WebClient webClient = new WebClient();
            webClient.OpenReadCompleted += new OpenReadCompletedEventHandler(webClient_OpenReadCompleted);
            webClient.OpenReadAsync(new Uri(url));
        }

        private string result;
        void backgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            label1.Content = result;
        }

        void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            GetOneResult();
        }
        string GetOneResult()
        {

        Method method = Method.GET;// new Method(); // TODO: Initialize to an appropriate value
            string url = "http://api.t.sina.com.cn/account/verify_credentials.xml?oauth_consumer_key=3587646579&oauth_nonce=3636566&oauth_signature_method=HMAC-SHA1&oauth_timestamp=1312015003&oauth_token=1c1b8e4800c0cc920df3cce7e24b3e66&oauth_version=1.0&oauth_signature=8Krw5M3Q8Pj75QoZ%2fx3%2f7D49Dl4%3d";//string.Empty; // TODO: Initialize to an appropriate value
            string postData = string.Empty;
            //result = Utility.WebRequestUseAsyncEnumerator(method, url, postData);
            WebClient webClient = new WebClient();
            webClient.OpenReadCompleted += new OpenReadCompletedEventHandler(webClient_OpenReadCompleted);
            webClient.OpenReadAsync(new Uri(url));
            return result;
        }

        void webClient_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
        {
            label1.Content="done";
        }
    }
}
